<template>
	<view class="main">
		<Head></Head>
		<view class="box-top">
			<view class="txt">{{data.name |  ellipsis}}</view>
			<view class="button">
				<u-button type="primary" class="btn" text="保存" color="#7ADFCA" v-if="data&& data.value !='2'"
					@tap="nav"></u-button>
			</view>
		</view>
		<view class="box-list">
			<scroll-view class="main-top" :style="'height:calc(100% - 10px - '+KeyboardHeight+'px );'" scroll-y="true"
				:scroll-top="scrollTop">
				<view class="box-bottom" v-for="(item,index) in listPhem" :key="index"
					v-if="(item.remarkc && item.remarkc.trim()) || (item.baseremark && item.baseremark.trim()) == '病人基本信息'?index>0:index>-1">
					<view class="box-title">{{item.remarkc || item.baseremark}}</view>
					<view class="box-hoscode" v-for="(el,ind) in item.list" :key="ind">
						<view class="hoscode-text" v-html="`${el.remarkc ? el.remarkc: el.ordername }`">
						</view>
						<view class="hoscode-input" v-if="el.xiaowenbendanwei"
							style="display: flex;align-items: center;width: 60%;">
							<u-input   @focus="focusValue(ind,index)"
								:adjust-position="false" v-model="el.defultremark" :customStyle="{border: '1px solid #dadbde !important'}">
							</u-input>
							<view v-if="el.xiaowenbendanwei" class="hoscode-text" style="width: 25%;">
								{{el.phunit}}
							</view>
						</view>
						<view class="hoscode-input" v-if="el.xiaowenben">
							<u-input   @focus="focusValue(ind,index)"
								:adjust-position="false" v-model="el.defultremark" :customStyle="{border: '1px solid #dadbde !important'}">
							</u-input>
						</view>
						<view class="hoscode-input" v-if="el.dawenben">
							<u-textarea v-model="el.defultremark"  autoHeight confirmType="done"
								@focus="focusValue(ind,index)" :adjust-position="false" :customStyle="{border: '1px solid #dadbde !important'}"></u-textarea>
						</view>
						<view class="hoscode-input" v-if="el.danxuan">
							<!-- 单选框 -->
						</view>
						<view class="hoscode-input" v-if="el.duoxuan">
							<!-- 多选框 -->
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import Head from '../../components/html/head.vue'
	import {
		PingGuMoBanNeirong_list,
		PingGuMoBanBiaoTi_list,
		PingGuJiLuBiaoTi_ZongHelist,
		PingGuJiLuNeiRong_ZongHelist
	} from '../../api/user.js'
	import {
		mapMutations,
		mapState
	} from "vuex";
	export default {
		components: {
			Head
		},
		data() {
			return {
				value: '',
				listPhem: [],
				listPhems: [],
				data: {},
				KeyboardHeight: 0,
				scrollTop: 0,
				VauleIdx: -1,
				alITFSD: [],
				numYels: 0
			}
		},
		watch: {
			alITFSD: {
				handler(newval) {
					console.log(newval);
					let num = 0
					console.log(this.alITFSD);
					this.alITFSD.map(item => {
						this.numYels += item * 55
					})
					console.log(this.numYels);
					this.$nextTick(() => {
						this.scrollTop = ((55 * this.VauleIdx) - this.VauleIdx) + this.numYels
						console.log(this.scrollTop, '2');
					})
				},
				// deep: true,
			}
		},
		computed: {
			...mapState('user', ['userInfo', 'passObj', 'scanningArray', 'deleteObj'])
		},
		mounted() {
			this.KeyboardHeightFun()
		},
		onLoad(e) {
			if (e.value == '2') {
				this.data = e
				this.PingGuJiLuNeiRong_ZongHelistFun()
			} else {
				this.data = e
				this.PingGuMoBanNeirong_listFun()
			}
		},
		filters: {
			//文字数超出时，超出部分使用...
			ellipsis(value) {
				if (!value) return ''
				if (value.length > 11) {
					return value.slice(0, 11) + '...'
				}
				return value
			}
		},
		methods: {
			async nav() {
				try {
					const user = uni.getStorageSync('userInfo')
					const obj = uni.getStorageSync('deleteObj')
					let array = this.listPhem.map((item, index) => {
						let a = item.list.map((el, ind) => {
							console.log(2);
							let inpds = el.remarkc + ':' + el?.defultremark ? el.defultremark : '' + el.phunit
							return {
								valuetype: el.valuetype,
								phcode: el.abc,
								phvalue: el.drcode,
								phrcd:'',
								phunit: el.phunit,
								prtype: this.data.value,
								relardn:'',
		
								remarkc: inpds ? inpds : '',
								setno: el.setno,
								inputfield: el?.defultremark ? el.defultremark : '',
								modeldrugrdn: '',
								markflag: el.markflag,
								setnomark: '',
								repeatflag: el.repeatflag,
								newflag: '',
								selectflag: el.selectflag,
								degree: '',
								Xiangtitle: '',
								modelid: el.modelid
							}
						})
						return a
					})
					console.log(array);
					let arrays = JSON.stringify(array[0])
					const data = {
						HosKind: user.hoscode,
						jsonarray: arrays,
						iModelId: this.listPhem[0].modelid,
						mrn: obj.MRN,
						series: obj.SERIES,
						bedid: obj.BEDID,
						iPrType: this.data.value,
						iTitle: this.data.name,
						sOperdept: user.HosKind,
						sOperJp: user.sUserid,
					}
					const res = await this.$http.ZongHeMoBanBaoCun(data)
					if (res[0].result == '1') {
						uni.showToast({
							title: '保存成功！',
							duration: 1000
						});

					} else {
						uni.showToast({
							title: '保存失败！',
							duration: 1000,
							icon: 'error'
						});
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			focusValue(value, numb) {
				console.log(value, numb);
				this.numYels = 0
				this.alITFSD = []
				console.log(this.listPhem);
				this.listPhem.map((item, index) => {

					if (index < numb) {
						if (item.remarkc?.trim() !== '病人基本信息') {
							this.alITFSD.push(item.list.length)
						}
					}
				})
				console.log(this.alITFSD, 'this.alITFSD');
				this.VauleIdx = value
			},
			KeyboardHeightFun(value) {
				if (this.VauleIdx == undefined) {
					return
				}
				uni.onKeyboardHeightChange(res => {
					this.KeyboardHeight = res.height
					if (res.height == 0) {
						// this.scrollTop = 0
						// this.numYels = 0
						// this.alITFSD = []
					}
				})
			},
			async PingGuMoBanNeirong_listFun() {
				try {
					uni.showLoading({
						title: '加载中...', //显示的文字
						// duration: 3000, //显示多少时间，默认1500毫秒
						icon: "loading", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					const user = uni.getStorageSync('userInfo')
					const obj = uni.getStorageSync('deleteObj')
					const data = {
						HosKind: user.hoscode,
						ModelId: this.data.id,
						mrn: obj.MRN,
						series: obj.SERIES,
						sOperdept: user.HosKind,
						sOperJp: user.sPassword
					}
					const res = await this.$http.PingGuMoBanNeirong_list(data)
					if (res) {
						this.listPhems = res
						this.PingGuMoBanBiaoTi_listFun()
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			async PingGuMoBanBiaoTi_listFun() {
				const user = uni.getStorageSync('userInfo')
				const obj = uni.getStorageSync('deleteObj')
				const data = {
					HosKind: user.hoscode,
					rdn: obj.MRN,
					ModelId: this.data.id,
					series: obj.SERIES,
					sOperdept: user.HosKind,
					sOperJp: user.sPassword
				}
				const res = await this.$http.PingGuMoBanBiaoTi_list(data)
				if (res) {
					let newsList = this.mapArray(this.listPhems)
					for (let i = 0; i < newsList.length; i++) {
						for (let j = 0; j < newsList[i].length; j++) {
							if (newsList[i][j].markflag != "" && newsList[i][j].markflag != '1' && newsList[i][j]
								.markflag != '2') {
								newsList[i][j].xiaowenben = true
								continue
							}
							if (newsList[i][j].markflag == '1') {
								if (newsList[i][j].remarkc?.indexOf("备注") ||
									newsList[i][j].remarkc?.indexOf("地址") ||
									newsList[i][j].remarkc?.indexOf("过去史") ||
									newsList[i][j].remarkc?.indexOf("主诉") ||
									newsList[i][j].remarkc?.indexOf("过敏史")) {
									// 大文本
									newsList[i][j].dawenben = true
									// CombinLinearlayout_DaEditText(context,
									// 	R.layout.hlpg_muban_dawenben_item,
									// 	hulipinggumobanneirong.get(j));
								} else if (newsList[i][j].markflag != "" && newsList[i][j].phunit != "") {
									// 小文本
									newsList[i][j].xiaowenbendanwei = true
									// CombinLinearlayout_EditText(context,
									// 	R.layout.hlpg_muban_xiaowenben_item,
									// 	hulipinggumobanneirong.get(j));
								} else {
									newsList[i][j].xiaowenben = true
								}
								continue;
							}
							/*
							 * 大文本
							 */
							if (newsList[i][j].markflag == '2') {
								newsList[i][j].dawenben = true
								// continue;
								// CombinLinearlayout_DaEditText(context,
								// 	R.layout.hlpg_muban_dawenben_item,
								// 	hulipinggumobanneirong.get(j));
								continue;
							}
							/*
							 * 单选
							 */
							if (newsList[i][j].selectflag == 'S') {
								newsList[i][j].danxuan = true
								// CombinLinearlayout_Spinner2(context,
								// 	hulipinggumobanneirong.get(j));
							}
							/*
							 * 多选
							 */
							else {
								newsList[i][j].duoxuan = true
								// CombinLinearlayout_Duoxuan(context,
								// 	hulipinggumobanneirong.get(j));
							}
						}
					}
					res.sort((a, b) => {
						return a.rdn - b.rdn
					})
					let arr = res.map((item, index) => {
						let setArray = newsList[index]?.filter(i => {
							return i.steppointer == item.rdn
						})
						return {
							...item,
							list: setArray
						}
					})
					let a = arr.filter(el => el.baseremark.trim() != '病人基本信息')
					this.listPhem = a
					this.$nextTick(() => {
						uni.hideLoading();
					})
				}
			},
			async PingGuJiLuNeiRong_ZongHelistFun() {
				try {
					uni.showLoading({
						title: '加载中...', //显示的文字
						// duration: 3000, //显示多少时间，默认1500毫秒
						icon: "loading", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					})
					const user = uni.getStorageSync('userInfo')
					const obj = uni.getStorageSync('deleteObj')
					const data = {
						HosKind: user.hoscode,
						rdn: this.data.id,
						mrn: obj.MRN,
						series: obj.SERIES,
						sOperdept: user.HosKind,
						sOperJp: user.sPassword
					}
					const res = await this.$http.PingGuJiLuNeiRong_ZongHelist(data)
					if (res) {
						this.listPhems = res
						this.PingGuJiLuBiaoTi_ZongHelistFun()
					}
				} catch (e) {
					//TODO handle the exception】
				}
			},
			async PingGuJiLuBiaoTi_ZongHelistFun() {
				const user = uni.getStorageSync('userInfo')
				const obj = uni.getStorageSync('deleteObj')
				const data = {
					HosKind: user.hoscode,
					rdn: this.data.id,
					mrn: obj.MRN,
					series: obj.SERIES,
					sOperdept: user.HosKind,
					sOperJp: user.sPassword
				}
				const res = await this.$http.PingGuJiLuBiaoTi_ZongHelist(data)
				if (res) {
					let newsList = this.mapArray(this.listPhems)
					console.log(newsList, 'newsListnewsList');
					for (let i = 0; i < newsList.length; i++) {
						for (let j = 0; j < newsList[i].length; j++) {
							if (newsList[i][j].markflag != "" && newsList[i][j].markflag != '1' && newsList[i][j]
								.markflag != '2' && newsList[i][j].phunit != "") {
								newsList[i][j].xiaowenbendanwei = true
								continue
							}
							if (newsList[i][j].markflag != "" && newsList[i][j].markflag != '1' && newsList[i][j]
								.markflag != '2' ) {
								newsList[i][j].xiaowenben = true
								continue
							}
							
							// console.log("体重                                    ".indexOf("备注"));
							if (newsList[i][j].markflag == '1') {
								if (newsList[i][j].remarkc.indexOf("备注") != -1 ||
									newsList[i][j].remarkc.indexOf("地址") != -1 ||
									newsList[i][j].remarkc.indexOf("过去史")  != -1||
									newsList[i][j].remarkc.indexOf("主诉") != -1 ||
									newsList[i][j].remarkc.indexOf("过敏史") != -1) {
									// 大文本
									newsList[i][j].dawenben = true
									// CombinLinearlayout_DaEditText(context,
									// 	R.layout.hlpg_muban_dawenben_item,
									// 	hulipinggumobanneirong.get(j));
								} else if (newsList[i][j].markflag != "" && newsList[i][j].phunit != "") {
									
									// 小文本
									newsList[i][j].xiaowenbendanwei = true
									// CombinLinearlayout_EditText(context,
									// 	R.layout.hlpg_muban_xiaowenben_item,
									// 	hulipinggumobanneirong.get(j));
								} else {
									newsList[i][j].xiaowenben = true
								}
								continue;
							}
							/*
							 * 大文本
							 */
							if (newsList[i][j].markflag == '2') {
								newsList[i][j].dawenben = true
								// continue;
								// CombinLinearlayout_DaEditText(context,
								// 	R.layout.hlpg_muban_dawenben_item,
								// 	hulipinggumobanneirong.get(j));
								continue;
							}
							/*
							 * 单选
							 */
							if (newsList[i][j].selectflag == 'S') {
								newsList[i][j].danxuan = true
								// CombinLinearlayout_Spinner2(context,
								// 	hulipinggumobanneirong.get(j));
							}
							/*
							 * 多选
							 */
							else {
								newsList[i][j].duoxuan = true
								// CombinLinearlayout_Duoxuan(context,
								// 	hulipinggumobanneirong.get(j));
							}
						}
					}
					let arr = res.map((item, index) => {
						return {
							...item,
							list: newsList[index]
						}
					})
					this.listPhem = arr
					this.$nextTick(() => {
						uni.hideLoading();
					})
				}
			},
			//处理数组的方法
			mapArray(target) {
				let obj = {},
					result = [];
				target.map((item) => {
					let key = this.data.value == '2' ? item.setno : item.steppointer;
					//如果有这个key，就push一个对象
					if (obj[key]) {
						obj[key].push(item);
						//如果没有这个key，就把对象设置成数组格式（方便后面push进去）
					} else {
						obj[key] = [item];
					}
				});
				//根据obj中有几个对象，挨个push进去，组成最后的数组
				for (const key in obj) {
					const element = obj[key];
					result.push(element);
				}
				return result;
			}
		}
	}
</script>


<style scoped lang="scss">
	.main {
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}

	// .main-box {

	.box-top {
		padding: 7px 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 21px;
		font-family: SourceHanSansCN-Normal, SourceHanSansCN;
		font-weight: 400;
		color: #333333;
		line-height: 32px;
		box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.06);

		.button {
			width: 84px;
		}

		.btn {
			box-shadow: 0px 2px 6px 0px #6AC8B5;
		}
	}

	.box-list {
		height: calc(100% - 165px);
		// overflow-y: scroll;
		padding: 10px 10px 0;
		overflow: hidden;
	}

	.box-bottom {
		width: 100%;
		// height: 100%;

		.box-title {
			font-size: 18px;
			font-family: SourceHanSansCN-Normal, SourceHanSansCN;
			font-weight: 400;
			color: #086168;
			line-height: 27px;
			margin-bottom: 10px;
		}

		.box-hoscode {
			padding-bottom: 10px;
			display: flex;

			// align-items: center;
			.hoscode-text {
				background: #B9CACB;
				// border-radius: 6px 6px 0px 0px;
				font-size: 15px;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 23px;
				padding: 6px 0;
				text-align: center;
				width: 40%;
				height: 26px;
				line-height: 18px;
			}

			.hoscode-input ::v-deep {
				width: calc(100% - 40%);

				.u-input {
					border-radius: 0px;
				}

				.u-textarea {
					border-radius: 0px;
					height: 96px;
					overflow: auto;
				}
			}
		}

	}

	// }

	.u-reset-button ::v-deep {
		.u-button__text {
			font-size: 36rpx !important;
			font-family: SourceHanSansCN-Medium, SourceHanSansCN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 54rpx;
		}
	}
</style>
